﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using fsw.Common;
using System.Text;

namespace fsw.Web.Manager
{
    public partial class List : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;
        protected Model.Admin model_admin;
        protected int _id;
        protected int _typeid;//管理员类型
        protected int _active;//是否锁定
        protected int _Pid;//项目id
        protected string _action = string.Empty;//操作类型
        protected void Page_Load(object sender, EventArgs e)
        {
            model_admin = GetAdminInfo();
            if (model_admin.TypeId != 1) Response.End();
            this._action = DTRequest.GetQueryString("action");
            this._id = DTRequest.GetQueryInt("id");
            if (this._action == "del" && this._id > 0)
            {
                if (model_admin.Id == this._id)
                {
                    Response.Write(JsonErr("error", "管理员删除失败，不能删除自已!"));
                    Response.End();
                }
                BLL.Admin bll_del = new BLL.Admin();
                Model.Admin model_del = bll_del.GetModel(this._id);
                if (model_del != null)
                {
                    model_del.isStatus = model_del.isStatus == 0 ? 1 : 0;
                    string ActiveText = model_del.isStatus == 0 ? "解锁" : "锁定";
                    if (bll_del.Update(model_del))
                    {
                        AdminAddLog(model_admin.Id, model_admin.UserName, ActiveText + model_del.UserName + "管理员成功!");
                        Response.Write(JsonErr("success", ActiveText + "管理员成功!"));
                        Response.End();
                    }
                }
            }
            this.keywords = DTRequest.GetQueryString("keywords");
            this.pageSize = GetPageSize(15); //每页数量
            this._typeid = DTRequest.GetQueryInt("tid");
            this._active = DTRequest.GetQueryInt("aid");
            if (!Page.IsPostBack)
            {
                string CombSqlTxtText = CombSqlTxt(this.keywords);
                RptBind("id > 0" + CombSqlTxtText, "LastTime desc");
            }
        }
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("Manager_list_page_size", "fswPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;
            BLL.Admin bll = new BLL.Admin();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("List.aspx", "keywords={0}&tid={1}&aid={2}&pid={3}&page={4}",
                this.keywords, this._typeid.ToString(), this._active.ToString(), this._Pid.ToString(), "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        /// <summary>
        /// 设置分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("Manager_list_page_size", "fswPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("List.aspx", "keywords={0}&tid={1}&aid={2}", this.keywords, this._typeid.ToString(), this._active.ToString()));
        }
        #endregion
        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("List.aspx", "keywords={0}&tid={1}&aid={2}", txtKeywords.Text.Trim(), this._typeid.ToString(), this._active.ToString()));
        }
        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (UserName like  '%" + _keywords + "%' or RealName like  '%" + _keywords + "%')");
            }
            if (this._typeid > 0)
            {
                strTemp.Append(" and TypeId=" + this._typeid);
            }
            if (this._active == 1)
            {
                strTemp.Append(" and isStatus=1");
            }
            else if (this._active == 2)
            {
                strTemp.Append(" and isStatus=0");
            }
            return strTemp.ToString();
        }
        #endregion
    }
}